package org.grottarossa.ui.core.casemanagement.menu;

import org.grottarossa.template.data.pojo.workflows.WorkflowList;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.template.drivers.DriverException.ErrorCode;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.container.WorkflowContainer;

import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Tree;

public class ServicesTree extends Tree {

	/**
	 *
	 */
	private static final long serialVersionUID = -6287383627113700954L;
	private final ApplicationSession m_appSession;

	public ServicesTree(ApplicationSession appSession, String applicationName, ItemClickListener listener) throws DriverException
	{
		this.m_appSession = appSession;
		WorkflowList workflowList = m_appSession.getBuilder().getWorkflowManagement().getServices(applicationName);

		if(workflowList.size()==0)
		{
			throw new DriverException("Services list null", ErrorCode.KO);
		}

		setContainerDataSource(new WorkflowContainer(workflowList));

		setItemCaptionPropertyId("WorkflowCaption");
		setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);

		for (Object id : rootItemIds()) {
            this.setChildrenAllowed(id, false);
        }

		setSelectable(true);
        setNullSelectionAllowed(false);
        addListener((ItemClickListener) listener);

	}
}


